<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class setting_nis extends CI_Controller {
	
	var $objectid = 'setting_nis';
	
	function __construct(){
		parent::__construct();
		$this->load->model('sd/setting/mdl_setting_nis');
		//$this->output->enable_profiler(TRUE);
	}
	
	function test(){
		$this->output->enable_profiler(TRUE);
	}
	
	function index(){
		$data['objectid'] = $this->objectid;
		$this->load->view('sd/setting/setting_nis', $data);
	}
	
	function grid(){
		$data = $this->mdl_setting_nis->get_data();
		echo $this->mdl_setting_nis->togrid($data['row_data'], $data['row_count']);
	}
	
	function add(){
		$data['objectid'] = $this->objectid;
		
		$r = $this->mdl_setting_nis->get_data_edit();
		
		$data['id'] = $r->row()->id;
		$data['awalan'] = $r->row()->awalan;
		$data['digit'] = $r->row()->digit;
		
		$this->load->view('sd/setting/setting_nis_form', $data);
	}
	
	function save($aksi){
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}

		# rules validasi form
		$this->form_validation->set_rules('awalan', 'Kode Awalan', 'trim|required|xss_clean');
		$this->form_validation->set_rules('digit', 'No Urut', 'trim|numeric|required|xss_clean');
        
		# message rules
		$this->form_validation->set_message('required', 'Field %s harus diisi.');
		$this->form_validation->set_message('numeric', 'Field %s harus diisi dengan angka.');

		$data['pesan_error'] = '';
		if ($this->form_validation->run() == FALSE){
			$data["pesan_error"].= trim(form_error('awalan',' ',' '))==''?'':form_error('awalan',' ',' ').'<br>';
			$data["pesan_error"].= trim(form_error('digit',' ',' '))==''?'':form_error('digit',' ',' ').'<br>';
			
		}else{
			if($aksi=="add"){ // add
				$result = $this->mdl_setting_nis->update($data);	
			}
		}
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
	}
	
	
}